//
//  CRJSpotlightView.m
//  CircleTest
//
//  Created by crj on 2017/2/24.
//  Copyright © 2017年 crj. All rights reserved.
//

#import "CRJSpotlightView.h"
#import "AppDelegate.h"

@interface CRJSpotlightView()

@property (nonatomic, assign) CGRect circleRect;
@property (nonatomic, assign) BOOL isClearn;
@property (nonatomic) UIView * maskImageView;

@end

@implementation CRJSpotlightView

+ (CRJSpotlightView *)showSpotlightViewWithCircleRect:(CGRect)circleRect maskImageView:(UIView*)maskImageView{

    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    CRJSpotlightView *view = [[CRJSpotlightView alloc] initWithCircleRect:circleRect maskImageView:maskImageView];
    
    
    [appDelegate.window addSubview:view];
    
    [appDelegate.window bringSubviewToFront:view];
    return view;
}



- (id)initWithCircleRect:(CGRect)circleRect maskImageView:(UIView*)maskImageView
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    self = [super initWithFrame:appDelegate.window.bounds];
    if (self) {
        _isClearn = NO;
        _circleRect = circleRect;
        _maskImageView = maskImageView;
    }
    return self;
}


- (void)drawRect:(CGRect)rect {

    

//    UIView * _alphaView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
//    _alphaView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
    
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds];
    
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect: _circleRect cornerRadius:_circleRect.size.height/2]bezierPathByReversingPath]];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    shapeLayer.path = path.CGPath;
    
    [self.layer setMask:shapeLayer];
    

}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];

}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self setNeedsDisplay];
    
    [self removeFromSuperview];
    [_maskImageView removeFromSuperview];
    
}

@end
